from typing import Self

from pydantic import Field, model_validator

from .base import Base
from .resume_id import ResumeEducationId


class BaseResumeEducation(Base):
    year: int = Field(..., description='Год')
    education_institution: str = Field(
        ...,
        description='Учебное заведение'
    )
    faculty: str = Field(..., description='Факультет')
    specialization: str = Field(..., description='Специализация')
    graduation_year: int = Field(..., description='Год окончания')

    @model_validator(mode='after')
    def check_year(self) -> Self:
        if self.graduation_year < self.year:
            raise ValueError(
                'Год окончание не может быть меньше года начала обучения!'
            )
        return self


class ResumeEducationCreate(BaseResumeEducation):
    pass


class ResumeEducationResult(BaseResumeEducation):
    id: ResumeEducationId = Field(
        ...,
        description='Уникальный идентификатор'
    )


class ResumeEducationUpdate(BaseResumeEducation):
    year: int | None = Field(None, description='Год')
    education_institution: str | None = Field(
        None,
        description='Учебное заведение'
    )
    faculty: str | None = Field(None, description='Факультет')
    specialization: str | None = Field(None, description='Специализация')
    graduation_year: int | None = Field(None, description='Год окончания')

    @model_validator(mode='after')
    def check_year(self) -> Self:
        if self.graduation_year is None or self.year is None:
            return self
        return super().check_year()  # noqa
